﻿using System;
using System.Data;
using System.Data.DBLib;
using SysComm;

namespace SysOperationLayer.DBHandle
{
    public class DBBase
    {
        protected CSqlDB m_objDB;
        protected string m_strMessage;

        public DBBase()
        {
            //connect the database;
        }

        ~DBBase()
        {
            //connect the database
        }

        public bool ConnectDB()
        {
            if (m_objDB == null || !m_objDB.isConnected())
            {
                if (m_objDB != null)
                {
                    m_objDB.close();
                }

                m_objDB = new CSqlDB();

                if (m_objDB.connect(SysUtil.m_strDBUserID, SysUtil.m_strDBPassWord, SysUtil.m_strDBDataBase, SysUtil.m_strDBServerIP))
                {
                }
                else
                {
                    m_strMessage = m_objDB.getLastError();
                    m_objDB = null;
                }
            }

            return m_objDB != null;
        }

        public CSqlDB GetDB()
        {
            return m_objDB;
        }

        public string ErrorMessage
        {
            get
            {
                return m_strMessage;
            }
            set
            {
                m_strMessage = value;
            }
        }

        public void CloseDB()
        {
            if (m_objDB != null)
            {
                m_objDB.close();
            }
        }

        public string GetValue(DataRow dr, string strField)
        {
            try
            {
                return dr[strField].ToString();
            }
            catch (Exception)
            {
            }

            return "";
        }

        public byte GetByteValue(DataRow dr, string strField)
        {
            try
            {
                return Convert.ToByte(dr[strField]);
            }
            catch (Exception)
            {
            }

            return 0;
        }

        public int GetIntValue(DataRow dr, string strField)
        {
            try
            {
                return Convert.ToInt32(dr[strField]);
            }
            catch (Exception)
            {
            }

            return 0;
        }

        public short GetInt16Value(DataRow dr, string strField)
        {
            try
            {
                return Convert.ToInt16(dr[strField]);
            }
            catch (Exception)
            {
            }

            return 0;
        }

        public double GetDoubleValue(DataRow dr, string strField)
        {
            try
            {
                return Convert.ToDouble(dr[strField]);
            }
            catch (Exception)
            {
            }

            return 0;
        }

        public DateTime GetDateTimeValue(DataRow dr, string strField)
        {
            try
            {
                return Convert.ToDateTime(dr[strField]);
            }
            catch (Exception)
            {
            }

            return new DateTime(1900, 1, 1);
        }

        public byte[] GetBinaryValue(DataRow dr, string strField)
        {
            try
            {
                return (byte[])dr[strField];
            }
            catch (Exception)
            {
            }

            return null;
        }

        public bool IsEmpty(string strValue)
        {
            return strValue == null || strValue == "";
        }
    }
}
